package com.zys.zys_mybatis.utils;

/**
 * @BelongsProject: ZYSMybatis
 * @BelongsPackage: com.zys.zys_mybatis.utils
 * @Author: ZYS
 * @CreateTime: 2024-05-27  18:40
 * @Description: TODO
 */
public class StringUtil {

    /**
     * 下划线转小驼峰
     * 1. 首字母小写
     * 2. 下划线后的字母大写
     *
     * @param param
     * @return String
     */
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == '_') {
                if (++i < len) {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     * 1. 首字母小写
     * 2. 下划线后的字母大写
     */
    public static String camelToUnderline(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append('_');
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 首字母大写
     *
     */
    public static String firstToUpperCase(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        char c = param.charAt(0);
        return Character.toUpperCase(c) + param.substring(1);
    }
}
